<template>
  <div class='xtx-goods-page' v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{goods.categories[0].name}}</XtxBreadItem>
        <transition name="fade-right" mode="out-in">
          <XtxBreadItem :key="goods.id">{{goods.name}}</XtxBreadItem>
        </transition>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
          <GoodsImage :images="goods.mainPictures"/>
          <GoodsSales/>
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
          <GoodsName :goods="goods"></GoodsName>
          <GoodsSku :key="goods.id" :goods="goods" @change="changeSku"></GoodsSku>
          <XtxNumbox  label="数量：" v-model="count" :max="goods.inventory" :min="1"></XtxNumbox>
          <XtxButton type="primary" style="margin-top:20px;" @click="addToCartFn">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs></GoodsTabs>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn></GoodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" :goods="goods"></GoodsHot>
          <GoodsHot :type="2" :goods="goods"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/product'
import GoodsRelevant from './components/goods-relevant'
import { useRoute } from 'vue-router'
import { provide, ref, watch } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import xtxMessage from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const goods = ref(null)
    const route = useRoute()
    // console.log(route.path)
    // 定义数量组件初始值
    const count = ref(1)
    // 因为vue是单页面应用，侦听路由变化发送请求
    watch(() => route.params.id, async id => {
      // 不是进入商品详情页面就直接结束
      if (route.path !== `/product/${id}`) return
      const { result } = await findGoods(id)
      console.log(result)
      goods.value = result
      count.value = 1
    }, { immediate: true })

    // 修改sku的函数
    const changeSku = (obj) => {
      // console.log(obj, 1111)
      goods.value.skuId = obj.skuId
      if (obj.skuId) {
        goods.value.inventory = obj.inventory
        goods.value.oldPrice = obj.oldPrice
        goods.value.price = obj.price
        goods.value.attrsText = obj.attrsText
      }
    }

    const store = useStore()
    // 添加到购物车按钮点击事件
    const addToCartFn = () => {
      // 如果没有skuId,说明规格没有选择完，提示用户
      if (!goods.value.skuId) return xtxMessage({ type: 'warn', message: '请选择完整规格' })
      // 拿到skuId后应该存储本地的数据
      const goodsItem = {
        // 原本goods的数据
        id: goods.value.spuCode, // SPUID
        name: goods.value.name, // 商品名称
        picture: goods.value.mainPictures[0], // 图片
        selected: true, // 是否选中
        stock: goods.value.inventory || goods.value.stock, // 库存
        // 组件筛选后返回的新数据
        price: goods.value.oldPrice, // 加入时价格
        skuId: goods.value.skuId, // SKUID
        attrsText: goods.value.attrsText, // 属性文字，例如“颜色:瓷白色 尺寸：8寸”
        nowPrice: goods.value.price, // 当前的价格
        // 数量组件的数据
        count: count.value, // 数量
        // isCollect: '', // 是否收藏
        // discount: '', // 折扣信息
        isEffective: true // 是否为有效商品
      }
      // console.log(goodsItem)
      // store.commit('cart/insertCart', goodsItem)
      // 仓库执行函数加入购物车
      store.dispatch('cart/insertCart', goodsItem).then(res => {
        xtxMessage({ type: 'success', message: res })
      })
    }
    // 传给后代的数据
    provide('goods', goods)
    return { goods, count, changeSku, addToCartFn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
